/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aksw.treelearning.utils;

import de.uni_leipzig.simba.fastngram.tokenizer.FastNGram;
import java.util.*;
import org.aksw.treelearning.tree.Condition;
import org.aksw.treelearning.data.Cache;
import org.aksw.treelearning.data.Instance;
import org.aksw.treelearning.data.Mapping;
import org.aksw.treelearning.data.MemoryCache;

/**
 *
 * @author ngonga
 */
public class Utils {

    public static Map<String, Set<String>> getData(Cache cache, String property) {
        Map<String, Set<String>> map = new HashMap<String, Set<String>>();
        ArrayList<String> uris = cache.getAllUris();
        for (String uri : uris) {
            TreeSet<String> values = cache.getInstance(uri).getProperty(property);
            for (String value : values) {
                if (!map.containsKey(value)) {
                    map.put(value, new HashSet<String>());
                }
                map.get(value).add(uri);
            }
        }
        return map;
    }

    /**
     * Computes a mapping given a source, a target and a condition
     *
     * @param source Source cache
     * @param target Target cache
     * @param condition Condition for computation
     * @return
     */
    public static Mapping computeMapping(Cache source, Cache target, Condition condition) {
        return computeMapping(source, target, condition.sourceProperty, condition.targetProperty, condition.threshold, condition.q);
    }

    public static Mapping computeMapping(Cache source, Cache target, String sourceProperty, String targetProperty, double threshold, int q) {

        Map<String, Set<String>> sourceMap = getData(source, sourceProperty);
        Map<String, Set<String>> targetMap = getData(target, targetProperty);
        Map<String, Map<String, Double>> map = FastNGram.compute(sourceMap.keySet(), targetMap.keySet(), q, threshold);
        Mapping result = new Mapping();
        for (String s : map.keySet()) {
            for (String t : map.get(s).keySet()) {
                for (String sourceUri : sourceMap.get(s)) {
                    for (String targetUri : targetMap.get(t)) {
                        result.add(sourceUri, targetUri, map.get(s).get(t));
                    }
                }
            }
        }
        return result;
    }

    public static void main(String args[]) {
        Cache source = new MemoryCache();
        Instance i;
        i = new Instance("ABBA");
        i.addProperty("label", "abba");
        source.addInstance(i);

        i = new Instance("ABBAA");
        i.addProperty("label", "abbaa");
        source.addInstance(i);

        i = new Instance("ABBAAA");
        i.addProperty("label", "abbaaa");
        source.addInstance(i);

        i = new Instance("ABBAAAB");
        i.addProperty("label", "abbaaab");
        source.addInstance(i);

        Condition condition = new Condition("label", 1, 3);
        System.out.println(computeMapping(source, source, condition));
    }
}
